﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using DA;
using Entities;

namespace ERP
{
    public partial class OrderForm : BaseERPPage
    {
        SupplierManager supplierManager = new SupplierManager();
        OrderManager orderManager = new OrderManager();
        OrderItemManager orderItemManager = new OrderItemManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListSupplier.DataSource = supplierManager.GetSuppliers();
                DropDownListSupplier.DataTextField = "Name";
                DropDownListSupplier.DataValueField = "SupplierId";
                DropDownListSupplier.DataBind();
                string productId = Request.QueryString.Get("OrderId");
                if (!string.IsNullOrEmpty(productId))
                {
                    LabelOrderNumber.Text = productId;
                    ERP_Orders orderFromDb = orderManager.GetOrder(int.Parse(productId));
                    DropDownListSupplier.SelectedValue = orderFromDb.SupplierId.ToString();
                    txtPurchseDescription.Text = orderFromDb.OrderDetails;
                    TextBoxReason.Text = orderFromDb.OrderReason;
                    LabelCurrentPrice.Text = orderManager.GetOrderPrice(int.Parse(productId)).ToString();
                    ItemsInOrder.DataSourceU = orderItemManager.GetItemsInOrder(int.Parse(productId));
                    ItemsInOrder.DataBind();
                }
            }
        }
        protected void btn_click(object sender, EventArgs e)
        {
            string orderId = Request.QueryString.Get("OrderId");
            if (!string.IsNullOrEmpty(orderId))  //Edit Order
            {
                if (orderManager.EditOrder(int.Parse(orderId), CollectOrderDetails()))
                {
                    orderManager.EditOrderStatus(int.Parse(orderId), 0, "");
                    Response.Redirect("MyOrders.aspx");
                    LabelAdded.Visible = true;
                }
            }

       
        }
        protected void btn_click_add_item_to_order(object sender, EventArgs e)
        {
            string orderId = Request.QueryString.Get("OrderId");
            if (!string.IsNullOrEmpty(orderId))  //Edit Order
            {
                if (orderManager.EditOrder(int.Parse(orderId), CollectOrderDetails()))
                {
                    Response.Redirect("AddItemToOrder.aspx?OrderId=" + orderId, true);
                }
            }
        }
        protected void onClickDeleteOrder(object sender, EventArgs e)
        {
            string orderId = Request.QueryString.Get("OrderId");
            if (!string.IsNullOrEmpty(orderId))  //Edit Order
            {
                if (orderManager.DeleteOrder(int.Parse(orderId)))
                {
                    Response.Redirect("MyOrders.aspx");
                }
            }
        }
        
        
        protected ERP_Orders CollectOrderDetails()
        {
            ERP_Orders order = new ERP_Orders();
            order.SupplierId = int.Parse(DropDownListSupplier.SelectedValue);
            order.OrderDetails = txtPurchseDescription.Text;
            order.OrderReason = TextBoxReason.Text;
            order.CreationDate = DateTime.Now;
            order.UserId = CurrentUser.UserId;
            order.Price = decimal.Parse(LabelCurrentPrice.Text);
            order.OrderStatus = (int) OrderStatus.טיוטא ;
            return order;
        }

        protected void OnClickButtonPrint(object sender, EventArgs e)
        {
            string orderId = Request.QueryString.Get("OrderId");
            if (!string.IsNullOrEmpty(orderId))
            {
                    Response.Redirect("ToPrintOrder.aspx?OrderId=" + orderId, true);
            }
        }
    }
}
